package com.realone.packman.codec.decoder;

import com.realone.packman.codec.decoder.vo.BmsDecoderDataVo12;
import com.realone.packman.codec.exception.BmsDecoderException;

import java.nio.ByteBuffer;

/**
 * 获取BMS电流 解码
 *
 * @author hugx
 * @create 2018-09-14 13:16
 */
public class BmsDecoder12 extends BmsDecoder<BmsDecoderDataVo12> implements BmsDecoderStrategy {

    public BmsDecoder12(Integer classType) {
        this.classType = classType;
    }

    @Override
    public BmsDecoderDataVo12 decoderBody(ByteBuffer bb) {
        if (bb == null) {
            throw new BmsDecoderException(new NullPointerException());
        }

        BmsDecoderDataVo12 result = new BmsDecoderDataVo12(this.classType);
        /* 解析电池包充放电状态和电流 */
        decoderCellRecharge(bb,result);
        return result;
    }

    /**
     * 解析电池包充放电状态和电流
     * @param bb
     * @param vo
     */
    private void decoderCellRecharge(ByteBuffer bb, BmsDecoderDataVo12 vo){
        short tmp=bb.getShort();
        vo.setRechargeState(tmp >>> 13);
        short electricitySizeTmp= (short) (tmp & 0x3FFF);
        byte hight= (byte) (((electricitySizeTmp >>> 8) & 0x3F) * 256);
        short low= (short) (electricitySizeTmp & 0x00FF);
        Double electricitySize= (hight + low) / 20.0;
        vo.setElectricitySize(electricitySize);
    }


}
